package net.xdclass.controller;


import io.jsonwebtoken.Claims;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import net.xdclass.enums.BizCodeEnum;
import net.xdclass.model.FlushToken;
import net.xdclass.model.LoginUser;
import net.xdclass.request.UserLoginRequest;
import net.xdclass.request.UserRegisterRequest;
import net.xdclass.service.IFileService;
import net.xdclass.service.IUserService;
import net.xdclass.service.impl.FileServiceImpl;
import net.xdclass.utils.JWTUtil;
import net.xdclass.utils.JsonData;
import net.xdclass.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;
import springfox.documentation.spring.web.json.Json;

import java.util.Map;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lixin
 * @since 2024-08-14
 */
@Api(tags = "用户模块")
@RestController
@RequestMapping("/api/user/v1")
public class UserController {

    @Autowired
    private IFileService fileService;

    @Autowired
    private IUserService userService;

    @Autowired
    private StringRedisTemplate redisTemplate;

    /**
     * 上传用户头像
     * 默认最大1M
     * @param file
     * @return
     */
    @ApiOperation("用户头像上传")
    @PostMapping("/upload")
    public JsonData uploadUserImage(
            @ApiParam(value = "文件上传" , required = true) MultipartFile file){

        String result = fileService.uploadUserImage(file);

        return result != null ? JsonData.buildSuccess(result) : JsonData.buildResult(BizCodeEnum.FILE_UPLOAD_USER_FAIL);
    }

    /**
     * 用户注册
     * @param registerRequest
     * @return
     */
    @ApiOperation("用户注册")
    @PostMapping("/register")
    public JsonData register(@ApiParam("用户注册对象") @RequestBody UserRegisterRequest registerRequest){

        JsonData jsonData = userService.register(registerRequest);
        return JsonData.buildSuccess(jsonData);
    }

    /**
     * 用户登录
     * @return
     */
    @ApiOperation("用户登录")
    @PostMapping("/login")
    public JsonData login(@ApiParam("用户登录对像") @RequestBody UserLoginRequest userLoginRequest){
        return userService.login(userLoginRequest);
    }

    /**
     * 刷新token的方案
     * @param param
     * @return
     */
    @PostMapping("/refresh_token")
    public JsonData getRefreshToken(Map<String, FlushToken> param){
        return userService.flushToken(param);
    }

    @ApiOperation("个人信息查询")
    @GetMapping("/detail")
    public JsonData detail(){
        UserVO userVO = userService.indUserDetail();

        return JsonData.buildSuccess(userVO);
    }
}
